package com.hymjweb.demo.framework.sys;

import com.hymjweb.demo.hymj.department_manage.bean.XtDepartmentPojo;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @BelongsProject: demo1
 * @BelongsPackage: com.hymjweb.demo.framework.sys
 * @Author: luoge
 * @CreateTime: 2019-10-20 00:01
 * @Description: 基础控制器，所以控制器继承该控制器
 */
@Controller
public class BaseController {

    @Autowired
    private BaseService baseService;


    private final Logger log = LoggerFactory.getLogger(getClass());

    /**
     * 获取Request对象
     * @return
     */
    public HttpServletRequest getRequest(){
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        return request;
    }

    public void setRequestBaseParam() {
        HttpServletRequest request = getRequest();
        String jdate = System.currentTimeMillis() + "";
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        request.setAttribute("base", basePath);
        request.setAttribute("jdate", jdate);
    }


    public void setRequestAttribute(String key, Object obj) {
        HttpServletRequest request = getRequest();
        request.setAttribute(key, obj);
    }

    public void printLogInfo(String obj) {
        log.info(obj);
    }


    /**
     * 获取当前登录用户对象
     *
     * @return
     */
    public XtUserPojo getLoginUser() {
        return baseService.getSystemUserPojo();
    }

    /**
     * 获取当前登录对象的归属部门
     *
     * @return
     */
    public XtDepartmentPojo getLoginUserDepartment() {
        return baseService.getSystemUserDepartment();
    }

    public String getLoginUserId() {
        return baseService.getSystemUserPojo().getId();
    }


    public Object objectToJSONObject(Object object) {
        return baseService.objectToJSONObject(object);
    }

    public String getSysStrId() {
        return baseService.getSysStrId();
    }

    /**
     * 设置 查询参数 部门ID及登录人 ID
     *
     * @param param
     */
    public void setQueryCommParam(Map param) {
        baseService.setQueryCommParam(param);
    }


    /**
     * 直接抛异常
     * @param exceptionDesc
     * @throws Exception
     */
    public void throwException(String exceptionDesc) throws Exception {
        baseService.throwException(exceptionDesc);
    }


}
